package fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.jianke.R;
import com.jianke.R2;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.bean.CustomData;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.log.LogUtil;
import com.lib.fastkit.views.load_state_view.MultiStateView;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.fastkit.views.spring_refresh.container.DefaultFooter;
import com.lib.fastkit.views.spring_refresh.container.DefaultHeader;
import com.lib.fastkit.views.spring_refresh.widget.SpringView;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.ui.fragment.BaseAppFragment;
import com.lib.utls.glide.GlideConfig;
import com.lib.view.banner.MyBannerView;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import bean.AllClassBean;
import bean.ChangeJingtTiaoBean;
import butterknife.BindView;

public class AllClassFragment_1 extends BaseAppFragment {

    @BindView(R2.id.allclass_banner)
    MyBannerView allclassBanner;
    @BindView(R2.id.recy_shiting)
    RecyclerView recyShiting;
    @BindView(R2.id.recy_zhuanjiang)
    RecyclerView recyZhuanjiang;
    @BindView(R2.id.tv_change)
    TextView tvChange;
    @BindView(R2.id.recy_jingtiao)
    RecyclerView recyJingtiao;
    @BindView(R2.id.springView)
    SpringView springView;
    @BindView(R2.id.state_view)
    MultiStateView stateView;

    //试听课、专讲课、精挑课的适配器
    ExpandClassAdapter expandClassAdapter;
    ZhuanJiangAdapter zhuanJiangAdapter;
    JingTiaoAdapter jingTiaoAdapter;

    //装载bannner数据的容器
    private List<CustomData> mList = new ArrayList<>();
    //装载试听课、专讲课、精挑课的容器
    private List<AllClassBean.ObjBean.ExpandClassBean> expandList = new ArrayList<>();
    private List<AllClassBean.ObjBean.LectureListBean> zhuangJList = new ArrayList<>();
    private List<AllClassBean.ObjBean.PickUpBean> jingTiaoListList = new ArrayList<>();

    @Override
    protected void onCreateView(View view, Bundle savedInstanceState) {

        stateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        stateView.setMultiStateViewLisener(new MultiStateView.MultiStateViewLisener() {
            @Override
            public void onTryAgain() {
                initData();
            }
        });

        initView();
        initData();

    }

    private void initData() {

        HttpUtils.with(getActivity())
                .addParam("requestType", "TIPCLASS_RECOMMEND_LIST")
                .execute(new HttpNormalCallBack<AllClassBean>() {
                    @Override
                    public void onSuccess(AllClassBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {

                            initPublicClass(result.getObj().getPublic_class());
                            setShiTingData(result);
                            setZhuanJiangData(result);
                            setJingTiaoData(result.getObj().getPick_up());

                            stateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);

                        } else {
                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                            showToast(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(String e) {
                        if (stateView != null) {
                            stateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
                        }
                    }
                });

    }

    private void setJingTiaoData(List<AllClassBean.ObjBean.PickUpBean> result) {

        jingTiaoListList.clear();
        jingTiaoListList.addAll(result);
        jingTiaoAdapter.updateData(jingTiaoListList);
        jingTiaoAdapter.notifyDataSetChanged();
    }

    private void setZhuanJiangData(AllClassBean result) {

        zhuangJList.clear();
        zhuangJList.addAll(result.getObj().getLecture_list());
        zhuanJiangAdapter.updateData(zhuangJList);
        zhuanJiangAdapter.notifyDataSetChanged();

    }

    private void setShiTingData(AllClassBean result) {

        expandList.clear();
        expandList.addAll(result.getObj().getExpand_class());
        expandClassAdapter.updateData(expandList);
        expandClassAdapter.notifyDataSetChanged();
    }

    private void initPublicClass(final List<AllClassBean.ObjBean.PublicClassBean> public_class) {

        mList.clear();

        for (int i = 0; i < public_class.size(); i++) {
            mList.add(new CustomData(public_class.get(i).getCover(), "", "JianKeBanner", true));
        }
        allclassBanner.setupdateData(mList, MyBannerView.TYPE_RECTANGULAR);

        allclassBanner.setMyBannerViewListener(new MyBannerView.MyBannerViewListener() {
            @Override
            public void onBannerClick(int positon) {
//                goToClass(public_class.get(positon).getCourse_type(), public_class.get(positon).getCourse_id());
            }
        });
    }

    private void initView() {

        springView.setHeader(new DefaultHeader(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));
        springView.setEnableFooter(false);
        springView.setEnableHeader(false);

        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                initData();
                springView.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {
                springView.onFinishFreshAndLoad();
            }
        });

        //初始化三个播放容器
        initShiTingView();
        initZhuanJiangView();
        initJingTiaoView();


        tvChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                changeJingTiao();
            }
        });

    }


    /**
     * 点击换一批
     */
    private void changeJingTiao() {

        HttpUtils.with(getActivity())
                .addParam("requestType", "TIPCLASS_PICKUP_CHANGEBATCH")
                .execute(new HttpNormalCallBack<ChangeJingtTiaoBean>() {
                    @Override
                    public void onSuccess(ChangeJingtTiaoBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {

                            List<AllClassBean.ObjBean.PickUpBean> pickList = new ArrayList<>();
                            for (int i = 0; i < result.getObj().size(); i++) {
                                AllClassBean.ObjBean.PickUpBean pickUpBean = new AllClassBean.ObjBean.PickUpBean();
                                pickUpBean.setCover(result.getObj().get(i).getCover());
                                pickUpBean.setTcid(result.getObj().get(i).getTcid());
                                pickUpBean.setTitle(result.getObj().get(i).getTitle());
                                pickUpBean.setTitle_type(result.getObj().get(i).getTitle_type());
                                pickUpBean.setVideo_num(result.getObj().get(i).getVideo_num());
                                pickList.add(pickUpBean);
                            }
                            setJingTiaoData(pickList);
                        }
                    }

                    @Override
                    public void onError(String e) {
                    }
                });

    }

    private void initZhuanJiangView() {

        zhuanJiangAdapter = new ZhuanJiangAdapter(getActivity(), zhuangJList);
        recyZhuanjiang.setLayoutManager(new MyLinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        recyZhuanjiang.setNestedScrollingEnabled(false);
        recyZhuanjiang.setAdapter(zhuanJiangAdapter);

    }

    private void initJingTiaoView() {

        jingTiaoAdapter = new JingTiaoAdapter(getActivity(), jingTiaoListList);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 4, GridLayoutManager.VERTICAL, false);
        recyJingtiao.setLayoutManager(manager);
        recyJingtiao.setNestedScrollingEnabled(false);
        recyJingtiao.setAdapter(jingTiaoAdapter);

    }


    private void initShiTingView() {

        expandClassAdapter = new ExpandClassAdapter(getActivity(), expandList);
        recyShiting.setLayoutManager(new MyLinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        recyShiting.setNestedScrollingEnabled(false);
        recyShiting.setAdapter(expandClassAdapter);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_allclass;
    }


    //-------------------------------------------------------------------------------------试听课推荐
    class ExpandClassAdapter extends BaseAdapter<AllClassBean.ObjBean.ExpandClassBean> {

        public ExpandClassAdapter(Context context, List<AllClassBean.ObjBean.ExpandClassBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_shiting_item_img;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<AllClassBean.ObjBean.ExpandClassBean> mData) {

//            getItemSize(holder);

            //获取类型，是显示全图片还是图片+文字
            int type = mData.get(position).getTitle_type();

            if (1 == type) {//1表示显示图片+文字
                holder.getView(R.id.lin_content).setVisibility(View.VISIBLE);
                holder.getView(R.id.lin_allImg).setVisibility(View.GONE);
                ImageView itemImg = holder.getView(R.id.iv_shiting_img);
                Glide.with(getActivity())
                        .load(mData.get(position).getCover())
                        .apply(GlideConfig.getRoundOptions1(20))
                        .into(itemImg);
                holder.setText(R.id.tv_shiting_title, mData.get(position).getTitle());
                holder.setText(R.id.tv_shiting_classNum, "共" + mData.get(position).getVideo_num() + "节");

                holder.getView(R.id.lin_content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        LogUtil.e("传递的tcid的值为：" + mData.get(position).getTcid() + "");

                        ARouter.getInstance().build(ARouterPathUtils.YouXuan_ClassDetailActivity)
                                .withString("tcid", mData.get(position).getTcid() + "").navigation();

                    }
                });
            } else if (0 == type) {
                ImageView allImg = holder.getView(R.id.iv_shiting_allImg);
                Glide.with(getActivity())
                        .load(mData.get(position).getCover())
                        .apply(GlideConfig.getRoundOptions1(20))
                        .into(allImg);
                holder.getView(R.id.lin_allImg).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LogUtil.e("传递的tcid的值为：" + mData.get(position).getTcid() + "");
                        ARouter.getInstance().build(ARouterPathUtils.YouXuan_ClassDetailActivity)
                                .withString("tcid", mData.get(position).getTcid() + "").navigation();
                    }
                });

            }
        }

    }


    //-------------------------------------------------------------------------------------专讲课推荐
    class ZhuanJiangAdapter extends BaseAdapter<AllClassBean.ObjBean.LectureListBean> {

        public ZhuanJiangAdapter(Context context, List<AllClassBean.ObjBean.LectureListBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_zhuanjiang;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<AllClassBean.ObjBean.LectureListBean> mData) {
            //获取类型，是显示全图片还是图片+文字
            int type = mData.get(position).getTitle_type();

            if (1 == type) {//1表示显示图片+文字
                holder.getView(R.id.lin_content).setVisibility(View.VISIBLE);
                holder.getView(R.id.lin_allImg).setVisibility(View.GONE);
                ImageView itemImg = holder.getView(R.id.iv_zhuanj_img);
                Glide.with(getActivity())
                        .load(mData.get(position).getCover())
                        .apply(GlideConfig.getRoundOptions1(20))
                        .into(itemImg);
                holder.setText(R.id.tv_zhuanj_title, mData.get(position).getTitle());
                holder.setText(R.id.tv_zhuanj_classNum, "共" + mData.get(position).getVideo_num() + "节");

                holder.getView(R.id.lin_content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LogUtil.e("传递的tcid的值为：" + mData.get(position).getTcid() + "");
                        ARouter.getInstance().build(ARouterPathUtils.YouXuan_ClassDetailActivity)
                                .withString("tcid", mData.get(position).getTcid() + "").navigation();
                    }
                });

            } else if (0 == type) {
                ImageView allImg = holder.getView(R.id.iv_zhuanj_allImg);
                Glide.with(getActivity())
                        .load(mData.get(position).getCover())
                        .apply(GlideConfig.getRoundOptions1(20))
                        .into(allImg);
                holder.getView(R.id.lin_allImg).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LogUtil.e("传递的tcid的值为：" + mData.get(position).getTcid() + "");
                        ARouter.getInstance().build(ARouterPathUtils.YouXuan_ClassDetailActivity)
                                .withString("tcid", mData.get(position).getTcid() + "").navigation();
                    }
                });

            }
        }
    }


    //-------------------------------------------------------------------------------------精挑课推荐
    class JingTiaoAdapter extends BaseAdapter<AllClassBean.ObjBean.PickUpBean> {

        public JingTiaoAdapter(Context context, List<AllClassBean.ObjBean.PickUpBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_jingtiao;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<AllClassBean.ObjBean.PickUpBean> mData) {

            ImageView ishowImg = holder.getView(R.id.iv_jingtiao_img);
            Glide.with(getActivity())
                    .load(mData.get(position).getCover())
                    .apply(GlideConfig.getRoundOptions1(20))
                    .into(ishowImg);

            holder.setText(R.id.tv_jingtiao_title, mData.get(position).getTitle());
            holder.setText(R.id.tv_jingtiao_classNum, "共" + mData.get(position).getVideo_num() + "节");

            holder.getView(R.id.lin_jingtiao_item).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    LogUtil.e("传递的tcid的值为：" + mData.get(position).getTcid() + "");
                    ARouter.getInstance().build(ARouterPathUtils.YouXuan_ClassDetailActivity)
                            .withString("tcid", mData.get(position).getTcid() + "").navigation();

                }
            });
        }
    }


}
